package com.boco.modules.fdoc.web.outpatient;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Pattern.Flag;

import org.glassfish.grizzly.websockets.BaseWebSocketFilter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.service.OutpatientSourceService;
import com.boco.modules.fdoc.vo.BizOutpatientSourceVo;

/**
 * Description: 预约挂好号源管理
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/outpatientSource",produces = "application/json;charset=UTF-8")
public class OutpatientSourceController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	OutpatientSourceService sourceService;
	
	/**
	 * 跳转主页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/outpatient/outpatientSource2";
	}
	
	/**
	 * 获取当前的号源信息
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param startTime 查询开始时间
	 * @param endTime 查询结束时间
	 * @param clinicTime 医生坐诊日期
	 * @param pageIndex 页码
	 * @param pageSize 页面大小
	 * @return
	 * @throws ParseException 日期转换异常
	 */
	@ResponseBody
	@RequestMapping(value = "/getSourceList",method = RequestMethod.GET)
	public String getSourceList(HttpServletRequest request, Model model,String orgId,String startTime,String endTime,
					String clinicTime ,Integer pageIndex,Integer pageSize) throws ParseException{
		BizOutpatientSourceVo querybvo=new BizOutpatientSourceVo();
		Page<BizOutpatientSourceVo> page=new Page<BizOutpatientSourceVo>(pageIndex, pageSize);
		querybvo.setPage(page);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(startTime!=null && !"".equals(startTime)){
			Date date1=sdf.parse(startTime);
			Calendar stTime=Calendar.getInstance();
			stTime.set(Calendar.YEAR, date1.getYear()+1900);
			stTime.set(Calendar.MONTH, date1.getMonth());
			stTime.set(Calendar.DAY_OF_MONTH,date1.getDate());
			stTime.set(Calendar.HOUR_OF_DAY, 0);  
			stTime.set(Calendar.MINUTE, 0);  
			stTime.set(Calendar.SECOND, 0);
			stTime.set(Calendar.MILLISECOND, 0);
			Date sdate=new Date(stTime.getTime().getTime());
			querybvo.setQueryStartTime(sdate);
		}
		if(endTime!=null && !"".equals(endTime)){
			Date date2=sdf.parse(endTime);
			Calendar edTime=Calendar.getInstance();
			edTime.set(Calendar.YEAR, date2.getYear()+1900);
			edTime.set(Calendar.MONTH, date2.getMonth());
			edTime.set(Calendar.DAY_OF_MONTH,date2.getDate());
			edTime.set(Calendar.HOUR_OF_DAY, 23);  
			edTime.set(Calendar.MINUTE, 59);  
			edTime.set(Calendar.SECOND, 59);
			edTime.set(Calendar.MILLISECOND, 999);
			Date edate=new Date(edTime.getTime().getTime());
			querybvo.setQueryEndDate(edate);
		}
		if(clinicTime!=null && !"".equals(clinicTime)){
			querybvo.setClinicTime(clinicTime);
		}
		if(!"".equals(orgId) && orgId!=null){
			querybvo.setOrgId(orgId);
		}
		
		List<BizOutpatientSourceVo> list=sourceService.getSourceList(querybvo);
		Integer count =sourceService.getSourceCount(querybvo);
		return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
	}
	
	/**
	 * 跳转修改页面
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toUpdateSource",method = RequestMethod.GET)
	public String toUpdateSource(HttpServletRequest request, Model model,Integer id){
		BizOutpatientSourceVo bVo=sourceService.getSourceById(id);
		model.addAttribute("bVo", bVo);
		return "/outpatient/updateSource";
	}
	
	/**
	 * 修改号源信息 (修改过，即有人预定此号源时无法再做修改)
	 * @param request
	 * @param model
	 * @param id 号源id
	 * @param clinicDate 坐诊日期
	 * @param clinicTime 坐诊时间段
	 * @param remainderNumber 剩余号源数
	 * @return
	 * @throws ParseException 日期转换异常
	 */
	@ResponseBody
	@RequestMapping(value = "/updateSource",method = RequestMethod.POST)
	public String updateSource(HttpServletRequest request, Model model,Integer id,String clinicDate,String clinicTime,
			Integer remainderNumber) throws ParseException{
		BizOutpatientSourceVo bVo=sourceService.getSourceById(id);
		if(bVo.getVersion()>0){
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.HAS_ORDERED.getKey(), ApiStatusEnum.HAS_ORDERED.getValue()));
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date clinic=sdf.parse(clinicDate);
		BizOutpatientSourceVo searchVo=new BizOutpatientSourceVo();
		searchVo.setClinicDate(clinic);
		searchVo.setClinicTime(clinicTime);
		searchVo.setOrgId(bVo.getOrgId());
		
		Integer rows=sourceService.getSourceHased(searchVo);
		if(rows>0){
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.SOURCE_REPEAT.getKey(), ApiStatusEnum.SOURCE_REPEAT.getValue())); 
		}
		
		Integer registrationLimit=bVo.getRegistrationLimit();
		Integer currentNumber=bVo.getCurrentNumber();
		Integer orderLimit=bVo.getOrderLimit();
		Integer remainderNumber2=bVo.getRemainderNumber();
		Integer remainderOff=remainderNumber-remainderNumber2;
		orderLimit=orderLimit+remainderOff;
		registrationLimit=registrationLimit+remainderOff;
		
		
		bVo.setClinicDate(clinic);
		bVo.setClinicTime(clinicTime);
		bVo.setOrderLimit(orderLimit);
		bVo.setRegistrationLimit(registrationLimit);
		bVo.setRemainderNumber(remainderNumber);
		
		Integer flag=sourceService.updateSource(bVo);
		if(flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null)); 
		}else{
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 跳转新增号源界面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAddSource",method = RequestMethod.GET)
	public String toAddSource(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/outpatient/addSource";
	}
	
	/**
	 * 保存号源
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param sden 医院信息
	 * @return
	 * @throws ParseException
	 */
	@Transactional
	@ResponseBody
	@RequestMapping(value = "/saveSource",method = RequestMethod.POST)
	public String saveSource(HttpServletRequest request, Model model,String orgId,String sden) throws ParseException{
		
		List<Map> returnMap=JsonUtils.getObjectList(sden, Map.class);
		BizOutpatientSourceVo saveBvo=new BizOutpatientSourceVo();
		Integer flag=1;
		i:for(Map<String, Object> m1:returnMap){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			String  clinicDateStr=(String) m1.get("clinicDateStr");
			Date clinicDate=sdf.parse(clinicDateStr);
			
			BizOutpatientSourceVo searchVo=new BizOutpatientSourceVo();
			searchVo.setClinicDate(clinicDate);
			searchVo.setClinicTime((String) m1.get("clinicTime"));
			searchVo.setOrgId(orgId);
			
			Integer rows=sourceService.getSourceHased(searchVo);
			if(rows>0){
				continue i;
			}
			
			saveBvo.setOrgId(orgId);
			saveBvo.setClinicDate(clinicDate);
			saveBvo.setClinicTime((String) m1.get("clinicTime"));
			saveBvo.setRegistrationLimit(Integer.valueOf((String)m1.get("registrationLimit")));
			saveBvo.setOrderLimit(Integer.valueOf((String)m1.get("orderLimit")));
			saveBvo.setCurrentNumber(0);
			saveBvo.setRemainderNumber(Integer.valueOf((String)m1.get("registrationLimit")));
			saveBvo.setVersion(0);
			
			flag=sourceService.addSource(saveBvo);
			if(flag==0){
				break i;
			}
			
		}
		if(flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null)); 
		}else{
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
}
